package jike.demo16.code03;

import jike.demo16.AlertRule;
import jike.demo16.Notification;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description:ApplicationContext 是一个单例类，负责 Alert 的创建、组装（alertRule 和
 * notification 的依赖注入）、初始化（添加 handlers）工作。
 * @date 2022/11/9 21:14
 */
public class ApplicationContext {

    private AlertRule alertRule;

    private Notification notification;

    private Alert alert = new Alert();

    public void initializeBeans() {
        alertRule = new AlertRule();
        notification = new Notification();
        alert.addAlertHandler(new TpsAlertHandler(alertRule, notification));
        alert.addAlertHandler(new ErrorAlertHandler(alertRule, notification));
    }

    public Alert getAlert() {
        return alert;
    }

    // 饿汉式单例
    private static final ApplicationContext instance = new ApplicationContext();



    public static ApplicationContext getInstance() {
        // 初始化对象
        instance.initializeBeans();
        return instance;
    }
}


